<template>
  <bar :totalQuestions="items.length" />
  <div class="list-container">
      <div class="list-item" v-for="(item, index) in items" :key="index">
          <div class="item-content" @click="openPreview">
              <h2 class="item-title"> {{ item.chapter }} {{ item.name }}</h2>
          </div>
          <div class="item-time">
            {{item.st}}
          </div>
      </div>
  </div>
  <div class="preview-buttons">
    <button class="submit-button" @click="create">
      新建预习
    </button>
  </div>
  <div style="display: flex; justify-content: center;">
    <Sidebar />
  </div>

  <el-dialog v-model="dialogFormVisible" title="请输入章节信息" width="80%">
    <span>章节编号:</span>
    <el-input v-model="chapter" style="width: 70%">
    </el-input>
    <div style="height: 2em"></div>
    <span>章节名称:</span>
    <el-input v-model="name" style="width: 70%">
    </el-input>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirmCreate">
          确认发布
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import Bar from './components/bar.vue';
import Sidebar from '@/components/Sidebar.vue';
import { ElMessage } from 'element-plus'
import router from '@/router'

export default {
    components: {
        Bar,
        Sidebar,
    },
    data() {
        return {
            items: [
              {
                name: '分治法的基本思想',
                date: '2024.10.18',
                chapter: '2.2',
                st: '已发布'
              },
              {
                name: '递归的概念',
                date: '2024.10.18',
                chapter: '2.1',
                st: '已发布'
              },
              {
                name: 'NP完全性理论',
                date: '2024.10.15',
                chapter: '1.3',
                st: '已发布'
              },
              {
                name: '算法复杂度分析',
                date: '2024.10.15',
                chapter: '1.2',
                st: '已发布'
              },
              {
                name: '算法与程序',
                date: '2024.10.15',
                chapter: '1.1',
                st: '已发布'
              }],
            selectedItems: [],
            dialogFormVisible: 0,
            name: '',
            st: '已发布',
            date: '2024.10.20',
          chapter: ''
        }
    },
    methods: {
      create () {
        this.dialogFormVisible = 1
      },
      confirmCreate () {
        this.items.unshift({
          name: this.name,
          date: this.date,
          chapter: this.chapter,
          st: this.st
        })
        this.dialogFormVisible = 0;
        ElMessage({
          type: 'success',
          message: '发布成功！'
        })
      },
      openPreview() {
        router.push('/analysis2')
      }
    }
}
</script>

<style scoped>

.list-container {
    display: flex;
    flex-direction: column;
    margin-top: 20px;
}

.list-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.1);
    margin-bottom: 8px;
}

.item-content {
    display: flex;
    flex-direction: column;
}

.item-title {
    font-size: 18px;
    font-weight: bold;
    margin: 0;
}

.item-time {
    font-size: 16px;
    color: #666;
}

.item-select {
    background-color: transparent;
    border: none;
    cursor: pointer;
}

.circle {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 1px solid #ccc;
    display: inline-block;
    margin-right: 10px;
}

.selected {
    background-color: #41A2FF;
}

.preview-buttons {
  display: flex;
  justify-content: center;
  gap: 10%
}

.submit-button {
    margin-top: 20px;
    background-color: #41A2FF;
    color: aliceblue;
    padding: 12px 20px;
    font-size: 16px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}
</style>
